﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace VideoArchive
{
    /// <summary>
    /// Interaction logic for Config_frm.xaml
    /// </summary>
    public partial class Config_frm : Window
    {
        bool move = false;
        private System.Drawing.Point pt = new System.Drawing.Point();

        public Config_frm()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {


            double leftpos = this.Left;
            while (this.Left < leftpos + this.Width - 50)
            {
                this.Left = this.Left + 10;
                Thread.Sleep(5);
            }

            foreach (Window wd in Application.Current.Windows)
            {
                if (wd.Title.Equals("VideoArchive"))
                {
                    foreach (Button bt in FindVisualChildren<Button>(wd))
                    {
                        if (bt.Name.Equals("button3"))
                        {
                            bt.Visibility = Visibility.Visible;
                        }
                    }
                    wd.Owner = null;
                }
            }



            Close();
        }

        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (move)
            {
                this.Left += (System.Windows.Forms.Control.MousePosition.X - this.pt.X);
                this.Top += (System.Windows.Forms.Control.MousePosition.Y - this.pt.Y);
                foreach (Window wd in Application.Current.Windows)
                {
                    if (wd.Title.Equals("VideoArchive"))
                    {
                        wd.Left += (System.Windows.Forms.Control.MousePosition.X - this.pt.X);
                        wd.Top += (System.Windows.Forms.Control.MousePosition.Y - this.pt.Y);
                    }
                }
                this.pt = System.Windows.Forms.Control.MousePosition;
            }
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            move = true;
            this.pt = System.Windows.Forms.Control.MousePosition;
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            move = false;
        }

        private void Window_MouseLeave(object sender, MouseEventArgs e)
        {
            move = false;
        }

        private void Window_MouseDown_1(object sender, MouseButtonEventArgs e)
        {

        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            ConfigDataBase cdb = new ConfigDataBase();
            cdb.sqlExecute("update config set mediaserver='" + textBox1.Text + "' where id=1");
        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            ConfigDataBase cdb = new ConfigDataBase();
            cdb.sqlExecute("update config set user='" + textBox2.Text + "' where id=1");
        }

        private void textBox3_KeyUp(object sender, KeyEventArgs e)
        {
            ConfigDataBase cdb = new ConfigDataBase();
            cdb.sqlExecute("update config set password='" + textBox3.Password + "' where id=1");
        }

        private void textBox4_KeyUp(object sender, KeyEventArgs e)
        {
            ConfigDataBase cdb = new ConfigDataBase();
            cdb.sqlExecute("update config set path='" + textBox4.Text + "' where id=1");
        }

        private void textBox5_KeyUp(object sender, KeyEventArgs e)
        {
            ConfigDataBase cdb = new ConfigDataBase();
            cdb.sqlExecute("update config set folder='" + textBox5.Text + "' where id=1");
        }

        private void Window_Activated(object sender, EventArgs e)
        {
           
        }
    }
}
